<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.itaoke.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  Caption <Caption@163.com> QQ 1674653
// +----------------------------------------------------------------------

namespace Wap\Controller;
use Common\Controller\FirstendController;

class ShareController extends FirstendController {
	
	public function _initialize() {
		parent::_initialize();

		$this->_smod = M('UserShare');
		$this->page_size=C('itk_wap_pernum');
	}
	
	public function index() {
		$where['a.status']=1;
		$_db=C('DB_PREFIX');
		$count = $this->_smod->alias("a")->where($where)->count();		
		$pager = $this->_pager($count, $this->page_size);
		$shares = $this->_smod->alias("a")->join("{$_db}user as b on a.uid=b.id")->field("a.*,b.username")->where($where)->order("id desc")->limit($pager->firstRow . ',' . $pager->listRows)->select();
		$show=$pager->wxshow();
		$this->assign('shares',$shares);
		$this->assign('page',$show);
		$this->assign('foot_curr','share');
		$this->_config_seo(C('itk_seo_config.share_index'));
		$this->assign('share_curr','index');
		$this->display();
	}

	public function praise(){
		if(!IS_AJAX) return;
		$id=I('id');
		if($id=='') return;
		$this->_smod->where(array(
			'id'=>$id
		))->setInc('praise');
		$this->ajaxReturn(1,'','');
	}
	
	public function rank(){
		$info = $this->visitor->get();
		$this->assign('info', $info);
		if(info){
			$where['uid']=$info['id'];
			$where['status']=1;
			$allscore=M('UserShare')->where($where)->sum('score');
			$this->assign('allscore',$allscore);
			$res=M('UserShare')->where($where)->field("sum(score) as scores,uid")->group("uid")->order("scores desc")->limit(10)->select();
			$this->assign('ranks',$res);
			for($i=0;$i<count($res);$i++){
				if($res[$i]['uid']==$info['id'])
					$this->assign('myrank',$i+1);
			}

		}
		unset($where['uid']);
		$where['status']=1;
		//计算积分和排行
		$res=M('UserShare')->where($where)->field("sum(score) as scores,uid")->group("uid")->order("scores desc")->limit(10)->select();
		$this->assign('ranks',$res);
		
		//计算次数排行
		$res=M('UserShare')->where($where)->field("count(1) as count,uid")->group("uid")->order("count desc")->limit(10)->select();
		$this->assign('cranks',$res);
		//计算月积数排行
		$st=mktime(0,0,0,date('m'),1,date('Y'));
		$et=mktime(23,59,59,date('m'),date('t'),date('Y'));
		$where['add_time'] =array('BETWEEN',array($st,$et));
		$res=M('UserShare')->where($where)->field("sum(score) as scores,uid")->group("uid")->order("scores desc")->limit(10)->select();
		$this->assign('yranks',$res);		
		$this->assign('share_curr','rank');
		$this->display();		
	}
}